package nc.bs.test.borrow.rule;

import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.impl.pubapp.pattern.data.vo.VOUpdate;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.test.bookdoc.BookdocVO;
import nc.vo.test.borrow.AggBorrowVO;
import nc.vo.test.borrow.BorrowBVO;

public class RebackBookRule implements IRule<AggBorrowVO>{

	@Override
	public void process(AggBorrowVO[] billvos) {
		
		if(billvos != null){
			for(AggBorrowVO billvo : billvos){
				processSingle(billvo);
			}
		}
	}

	private void processSingle(AggBorrowVO billvo) {
		
		BorrowBVO[] itemvos = (BorrowBVO[]) billvo.getChildrenVO();
		
		String[] bookpks = new String[itemvos.length];
		
		for(int i = 0; i < bookpks.length; i ++){
			bookpks[i] = (String) itemvos[i].getAttributeValue("pk_bookdoc");
		}
		
		VOQuery query = new VOQuery<BookdocVO>(BookdocVO.class);
		
		BookdocVO[] bookVOs = (BookdocVO[]) query.query(bookpks);
		
		for(BookdocVO bookVO : bookVOs){
			bookVO.setStatus(VOStatus.UPDATED);
			
			bookVO.setAttributeValue("isborrow", UFBoolean.TRUE);
		}
		
		new VOUpdate<BookdocVO>().update(bookVOs);
	}
}
